package com.um.pasantiaum.empresa.usuarios;

import java.util.ArrayList;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.parse.ParseUser;
import com.um.pasantiaum.R;
import com.um.pasantiaum.objetos.UserManager;
import com.um.pasantiaum.objetos.Usuario;

public class AdminUserFragment extends ListFragment {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);

		setRetainInstance(true);

		AdminUserAdapter adapter = new AdminUserAdapter(UserManager
				.getUserManager(ParseUser.getCurrentUser()).getUsers());
		setListAdapter(adapter);
	}

	private class AdminUserAdapter extends ArrayAdapter<Usuario> {

		public AdminUserAdapter(ArrayList<Usuario> prod) {
			super(getActivity(), 0, prod);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = getActivity().getLayoutInflater().inflate(
						com.um.pasantiaum.R.layout.list_item_product, null);
			}
			Usuario o = getItem(position);
			TextView nameView = (TextView) convertView
					.findViewById(com.um.pasantiaum.R.id.prodruct_name);
			nameView.setText(o.getName());
			TextView descriptionView = (TextView) convertView
					.findViewById(com.um.pasantiaum.R.id.prodruct_description);
			descriptionView.setText(o.getEmail());
			TextView priceView = (TextView) convertView
					.findViewById(com.um.pasantiaum.R.id.product_price);
			priceView.setText("");

			return convertView;
		}
	}
	
	@TargetApi(11)
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup parent,
			Bundle savedInstanceState) {
		View v = super.onCreateView(inflater, parent, savedInstanceState);
		ListView listView = (ListView) v.findViewById(android.R.id.list);

		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
			registerForContextMenu(listView);
		else {
			listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
			listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

				@Override
				public boolean onActionItemClicked(ActionMode mode,
						MenuItem item) {
					switch (item.getItemId()) {
					case com.um.pasantiaum.R.id.menu_item_delete_producto:
						AdminUserAdapter adapter = (AdminUserAdapter) getListAdapter();
						UserManager manager = UserManager
								.getUserManager(ParseUser.getCurrentUser());
						for (int i = adapter.getCount() - 1; i >= 0; i--) {
							if (getListView().isItemChecked(i)) {
								manager.removeUser(adapter.getItem(i));
							}
						}
						mode.finish();
						adapter.notifyDataSetChanged();
						return true;
					default:
						return false;
					}
				}

				@Override
				public boolean onCreateActionMode(ActionMode mode, Menu menu) {
					MenuInflater inflater = mode.getMenuInflater();
					inflater.inflate(
							com.um.pasantiaum.R.menu.producto_lista_contexto,
							menu);
					return true;
				}

				@Override
				public void onDestroyActionMode(ActionMode arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public boolean onPrepareActionMode(ActionMode mode, Menu item) {
					return false;
				}

				@Override
				public void onItemCheckedStateChanged(ActionMode arg0,
						int arg1, long arg2, boolean arg3) {
					// TODO Auto-generated method stub

				}

			});
		}
		return v;
	}
	
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(com.um.pasantiaum.R.menu.new_product_menu, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.new_product:
			Intent i = new Intent(getActivity(), NewUserActivity.class);
			startActivityForResult(i, 0);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		((AdminUserAdapter) getListAdapter()).notifyDataSetChanged();

	}
}
